package com.tuniu.agents.impl.trip.chain;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuniu.agents.advisor.ChatEnvironmentMemory;
import com.tuniu.agents.impl.trip.Extractor;
import com.tuniu.agents.impl.trip.OnlineSearcher;
import com.tuniu.agents.impl.trip.Recommend;

import lombok.extern.slf4j.Slf4j;

@Slf4j
/**
 * Trip Chain Assembly
 */
@Component
public class TripChain {
    public static final String NETWORK_MODE = "networkMode"; // Network mode Key
    
    @Autowired
    private Extractor extractor;

    @Autowired
    private OnlineSearcher tripSearchAgent;

    @Autowired
    private Recommend tripRecommendAgent;

    @Autowired
    private ChatEnvironmentMemory chatEnvironmentMemory;
    
    private TripChainHandler chainHandler;
    private ProcessMode currentMode;
    private NetworkMode currentNetworkMode;
    public TripChain() {
        this.currentMode = ProcessMode.STANDARD;
    }

    /**
     * Set processing mode and reinitialize the chain of responsibility
     */
    public void setProcessMode(ProcessMode mode, String conversationId) {
        this.currentMode = mode;
        initChain(conversationId);
    }

    /**
     * Initialize interaction mode: whether to connect to network
     */
    public void setNetworkMode(NetworkMode mode, String conversationId) {
        this.currentNetworkMode = mode;
        if(mode == NetworkMode.ONLINE){
            // In online mode, need to store in global variables
            chatEnvironmentMemory.add(conversationId, Map.of(NETWORK_MODE, NetworkMode.ONLINE.getValue()));
        }
    }

    /**
     * Initialize the chain of responsibility
     */
    private void initChain(String conversationId) {
        // Create handlers
        ExtractorHandler extractorHandler = new ExtractorHandler(extractor);
        OnlineSearcherHandler searchHandler = new OnlineSearcherHandler(tripSearchAgent);
        RecommendHandler recommendHandler = new RecommendHandler(tripRecommendAgent);
    
        // Dynamically build the chain of responsibility based on mode
        switch (currentMode) {
            case BASIC:
                // Basic mode: extractor only
                this.chainHandler = extractorHandler;
                break;
                
            case STANDARD:
                // Standard mode: extractor -> searcher
                extractorHandler.setNextHandler(searchHandler);
                this.chainHandler = extractorHandler;
                break;
                
            case ADVANCED:
                // Advanced mode: all handlers
                extractorHandler.setNextHandler(searchHandler);
                searchHandler.setNextHandler(recommendHandler);
                this.chainHandler = extractorHandler;
                // More advanced handlers can be added here
                break;
        }
        
        log.info("Chain of responsibility initialization completed, Conversation ID: {}, Current mode: {}", conversationId, currentMode);
    }

    public String call(String conversationId, String message) {
        try {
            log.info("Starting to process trip request, Conversation ID: {}, Message: {}, Processing mode: {}", 
                    conversationId, message, currentMode);
            String result = chainHandler.handle(conversationId, message);
            log.info("Trip chain processing completed, Conversation ID: {}, Result: {}", conversationId, result);
            return result;
        } catch (Exception e) {
            log.error("TripChainAgent: Error executing trip chain", e);
            return "Trip chain execution failed: " + e.getMessage();
        }
    }

} 